﻿using System.Collections.Generic;
using System.Linq;

namespace BUS.DAL
{
    public class SearchDAL
    {
        internal static List<SanPhamBUS> Search(SearchBUS searchBUS)
        {
            var db = DataAccess.Database;

            var result = from p in db.SanPhams
                         where (p.MaSanPham.Contains(searchBUS.KeyWord) || 
                         p.TenSanPham.Contains(searchBUS.KeyWord) || 
                         p.MoTa.Contains(searchBUS.KeyWord) || 
                         p.BaoHanh.Contains(searchBUS.KeyWord)) 
                         && p.Gia >= searchBUS.PriceFrom * 1000000 
                         && p.Gia <= searchBUS.PriceTo * 1000000
                         select new SanPhamBUS()
                         {
                             ID = p.ID,
                             MaSanPham = p.MaSanPham,
                             TenSanPham = p.TenSanPham,
                             LoaiSanPham = new LoaiSanPhamBUS()
                             {
                                 ID = p.LoaiSanPham.ID,
                                 TenLoai = p.LoaiSanPham.TenLoai
                             },
                             MoTa = p.MoTa,
                             Gia = (decimal)p.Gia,
                             Anh = p.Anh,
                             BaoHanh = p.BaoHanh,                             
                             SoTon = (int)p.SoTon
                         };
            return result.ToList<SanPhamBUS>();
        }
    }
}
